package com.jn.erp.web.api.interaction.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.common.utils.StringUtils;
import com.jn.erp.security.controller.BaseWebAuthController;
import com.jn.erp.web.api.common.entity.TLabel;
import com.jn.erp.web.api.common.service.TLabelService;
import com.jn.erp.web.api.course.service.TCourseOrderConfigService;
import com.jn.erp.web.api.interaction.entity.THomework;
import com.jn.erp.web.api.interaction.entity.TStudentHomework;
import com.jn.erp.web.api.interaction.entity.vo.THomeworkVo;
import com.jn.erp.web.api.interaction.service.THomeworkService;
import com.jn.erp.web.api.interaction.service.TStudentHomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wyp
 * @since 2024-09-29
 */
@RestController
@RequestMapping("//tHomework")
public class THomeworkController  extends BaseWebAuthController {
    @Autowired
    private THomeworkService tHomeworkService;
    @Autowired
    private TStudentHomeworkService tStudentHomeworkService;
    /**
     作业新增新建
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody THomeworkVo tHomeworkVo){
        //机构id
        tHomeworkVo.setOrgId(Long.parseLong(loginDeptAuth()) );
        //当前登录人id
        tHomeworkVo.setCreateUser(String.valueOf(getUserId()) );
        tHomeworkVo.setCreateDate(LocalDateTime.now() );
        tHomeworkVo.setIsDelete("0");
        Boolean boo = tHomeworkService.save(tHomeworkVo);
        List<Long> ids = tHomeworkVo.getIds();
        List<TStudentHomework> tStudentHomeworks = new ArrayList<>();
        if(!CollectionUtils.isEmpty(ids)){
            ids.stream().forEach(u->{
                TStudentHomework tsh = new TStudentHomework(Long.parseLong(loginDeptAuth()),u,tHomeworkVo.getId(),LocalDateTime.now(),String.valueOf(getUserId()),null,null,"0");
                tStudentHomeworks.add(tsh);
            });
        }
        boo = tStudentHomeworkService.saveBatch(tStudentHomeworks);
        return AjaxResult.success(boo);
    }


    /**
     作业修改
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody THomework tHomework){
        //机构id
        tHomework.setUpdateDate(LocalDateTime.now() );
        //当前登录人id
        tHomework.setUpdateUser(String.valueOf(getUserId()) );
        Boolean boo = tHomeworkService.updateById(tHomework);
        return AjaxResult.success(boo);
    }

    /**
     列表
     */
    @PostMapping("list")
    public AjaxResult list(@RequestBody THomework tHomework){

        List<THomework> list = tHomeworkService.list(new QueryWrapper<THomework>().eq("org_id",Long.parseLong(loginDeptAuth())).eq("is_delete","0").eq(!StringUtils.isEmpty(tHomework.getCreateUser()),"create_user",tHomework.getCreateUser()));
        return AjaxResult.success(list);
    }

    /**
     教室删除
     */
    @PostMapping("delete")
    public AjaxResult delete(@RequestBody THomework tHomework){
        //机构id
        tHomework.setUpdateDate(LocalDateTime.now() );
        //当前登录人id
        tHomework.setUpdateUser(String.valueOf(getUserId()) );
        tHomework.setIsDelete("1");
        Boolean boo = tHomeworkService.updateById(tHomework);
        return AjaxResult.success(boo);
    }
}

